package com.qys.livesMall.shop.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.common.utils.bean.BeanProUtils;
import com.qys.livesMall.common.utils.ip.IpUtil;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.shop.modal.dto.QueryAddressDTO;
import com.qys.livesMall.shop.modal.dto.QueryAddressVO;
import com.qys.livesMall.shop.modal.dto.shop.*;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.vo.ShopContractVO;
import com.qys.livesMall.shop.modal.vo.ShopSimpleVO;
import com.qys.livesMall.shop.modal.vo.ShopVO;
import com.qys.livesMall.shop.service.H2DataPackageService;
import com.qys.livesMall.shop.service.H2ShopBaseService;
import com.qys.livesMall.shop.service.H2ShopContractService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * web店铺基础
 *
 * @author makejava
 * @since 2023-05-26 10:28:08
 */
@RestController
@RequestMapping("/web/shop/")
public class WebH2ShopBaseController {
    /**
     * 服务对象
     */
    @Resource
    private H2ShopBaseService h2ShopBaseService;

    @Autowired
    private H2ShopContractService shopContractService;

    @Resource
    private H2DataPackageService dataPackageService;

    /**
     * 分页查询
     *
     * @param search 筛选条件
     * @return 查询结果
     */
    @PostMapping("list")
    public R<QysPage<ShopVO>> queryByPage(@RequestBody ShopSearch search) {
        return R.ok(this.h2ShopBaseService.list(search));
    }


    /**
     * 获取当前用户店铺
     *
     * @return 单条数据
     */
    @GetMapping
    public R<ShopVO> get() {
        return R.ok(this.h2ShopBaseService.get());
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R<H2ShopBase> queryById(@PathVariable("id") Long id) {
        return R.ok(this.h2ShopBaseService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param h2ShopBase 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public R add(@RequestBody AppAddShop h2ShopBase, HttpServletRequest request) {
        String ip = IpUtil.getIpAddr(request);
        return this.h2ShopBaseService.add(h2ShopBase,ip);
    }

    /**
     * 编辑数据
     *
     * @param shop 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public R edit(@RequestBody EditShop shop) {
        return R.ok(this.h2ShopBaseService.update(shop));
    }

    /**
     * 审核通过
     *
     * @param shopId 店铺Id
     * @return 编辑结果
     */
    @PostMapping("auditPass/{shopId}")
    public R auditPass(@PathVariable Long shopId) {
        return h2ShopBaseService.auditPass(shopId);

    }

    /**
     * 闭店店铺审核拒绝
     *
     * @param shopId 店铺Id
     * @return 编辑结果
     */
    @PostMapping("auditRefuse/{shopId}")
    public R auditRefuse(@PathVariable Long shopId) {
        return h2ShopBaseService.auditRefuse(shopId);

    }

    /**
     * 审核不通过
     *
     * @param auditShop 实体
     * @return 编辑结果
     */
    @PostMapping("auditNoPass")
    public R auditNoPass(@RequestBody AuditShop auditShop) {
        return h2ShopBaseService.auditNoPass(auditShop.getShopId(),auditShop.getAuditDesc());
    }


    /**
     * 店铺闭店通过审批
     * @param shopId
     * @return
     */
    @PutMapping(value = "/closeShopApprove/{shopId}")
    public R<?> closeShopApprove(@PathVariable Long shopId) {
        return h2ShopBaseService.closeShopApprove(shopId);
    }

    /**
     * 获取店铺合同
     * @param shopId
     * @return
     */
    @GetMapping(value = "/shopContract/{shopId}")
    public R<ShopContractVO> getContract(@PathVariable Long shopId) {
        return R.ok(shopContractService.getByShopId(shopId));
    }

    @Operation(summary = "设置店铺结算天数")
    @PostMapping(value = "/setShopSettlementDay")
    public R setShopSettlementDay(@RequestBody shopBaseSettlementDay shopBaseSettlementDay){
        return R.ok(h2ShopBaseService.setShopSettlementDay(shopBaseSettlementDay));
    }

    @PostMapping("/list/simple")
    @Operation(summary = "店铺列表下拉查询")
    public R<QysPage> listShopSimple(@RequestBody ShopSearch search){
        QysPage<H2ShopBase> resultPage = h2ShopBaseService.search(search);
        resultPage.setRecords(BeanProUtils.copyListPropertiesV2(resultPage.getRecords(),ShopSimpleVO.class));
        return R.ok(resultPage);
    }

    @PostMapping("/address")
    @Operation(summary = "获取地区列表")
    public R<List<QueryAddressVO>> address(@RequestBody QueryAddressDTO dto){
        List<QueryAddressVO> queryAddress = dataPackageService.queryAddress(dto);
        return R.ok(queryAddress);
    }
}

